﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseComponent;

public partial class ChangeMainMailEN : System.Web.UI.Page
{
    SupportMainMail mobjSupportMainMail
    {
        get
        {
            if (ViewState["mobjSupportMainMail"] == null)
                ViewState["mobjSupportMainMail"] = System.Guid.NewGuid().ToString();

            return Session[ViewState["mobjSupportMainMail"].ToString()] as SupportMainMail;
        }

        set
        {
            if (ViewState["mobjSupportMainMail"] == null)
                ViewState["mobjSupportMainMail"] = System.Guid.NewGuid().ToString();

            Session[ViewState["mobjSupportMainMail"].ToString()] = value;
        }
    }


    Persons mobjUser
    {
        get
        {
            if (ViewState["mobjUser"] == null)
                ViewState["mobjUser"] = System.Guid.NewGuid().ToString();

            return Session[ViewState["mobjUser"].ToString()] as Persons;
        }

        set
        {
            if (ViewState["mobjUser"] == null)
                ViewState["mobjUser"] = System.Guid.NewGuid().ToString();

            Session[ViewState["mobjUser"].ToString()] =value;
        }
    }


    /// <summary>
    /// this function will check the security and customize the page
    /// </summary>
    private void checkSecurityAndCustomizePage()
    {
        mobjUser = GlobalVars.getCurrentPerson();

        if ((!mobjUser.lstAllRulesIDs.Contains((long)RulesEnum.Support_control)) || mobjUser.UserTypeID != 1)
        {
            Response.Redirect("LoginEN.aspx");
        }

        SupportMainMailDB objSupportMainMailDB = new SupportMainMailDB(ConnectionStrings.LMS);
        List<SupportMainMail> lstSupportMainMail = objSupportMainMailDB.Select("", "");
        if (lstSupportMainMail.Count == 0)
        {
            Response.Redirect("LoginEN.aspx");
        }

        mobjSupportMainMail = lstSupportMainMail[0];
    }


    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            checkSecurityAndCustomizePage();


            txtEmail.Text = mobjSupportMainMail.Email;
        }

    }
    
    
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            SupportMainMailDB objSupportMainMailDB = new SupportMainMailDB(ConnectionStrings.LMS);
            mobjSupportMainMail.Email = txtEmail.Text;

            objSupportMainMailDB.Update(mobjSupportMainMail);

            lblResult.Text = "Mail updated successfully.";
        }
    }
}